package com.tianyi.sfzy1.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tianyi.sfzy1.domain.Audio;
import com.tianyi.sfzy1.mapper.AudioMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AudioService {
    @Autowired
    private AudioMapper audioMapper;
    @Autowired
    private CourseTypeService courseTypeService;
    @Autowired
    private ConsultantService consultantService;
   //通过id删除音频
//    public int deleteAudio(Long id){
//        return audioMapper.deleteByPrimaryKey(id);
//    }
   public int deleteAudios(List<Integer> audioIds) {
       return audioMapper.deleteByAudioIds(audioIds);
   }
    //通过id查询音频
    public Audio findAllAudio(Long id){
        return audioMapper.selectByPrimaryKey(id);
    }
    //添加音频
    public int addAudio(Audio audio){
        return audioMapper.insertSelective(audio);
    }
    //修改音频状态
    public int updateAudioStatus(Audio audio){
        return audioMapper.updateByPrimaryKeySelective(audio);
    }

    //查找音频和课程类型和咨询师相关联的表
    public List<Audio> findAudioWithCourseTypeAndConsultantByFields(
            String title,
            String courseTypeName,
            String consultantRealname,
            String audioId) {
        return audioMapper.selectAudioWithCourseTypeAndConsultantByFields(
                title,
                courseTypeName,
                consultantRealname,
                audioId);
    }
    public PageInfo<Audio> findByPage(int pageNum,int pageSize,Audio audio){
        PageHelper.startPage(pageNum,pageSize);
        List<Audio> list = audioMapper.selectAll(audio);
        list.forEach(e -> {
            e.setCoursetype(courseTypeService.findById(Long.valueOf(e.getTypeId())));
            e.setConsultant(consultantService.findById(Long.valueOf(e.getConsultantId())));
        });
        PageInfo<Audio> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

}
